<?php
require_once("db.php");
require_once("einfo/0320/not_exist_db_user.php");
require_once("function.php");

$from_name=$_POST["fromname"];
$from_email=$_POST["fromemail"];
$subject=$_POST["subject"];
$content=$_POST["content"];
$readdb=$_POST["readdb"];
$readfile=$_POST["readfile"];

$other_emails=trim($_POST["other_emails"]);
if($other_emails!="")
	$ary_other_emails=explode(",",$other_emails);
	
$filter_emails=trim(strtolower($_POST["filter_emails"]));
if($filter_emails!="")
	$ary_filter_emails=explode(",",$filter_emails);
	
$str_tmp="";
$str_filter="";
$cf=0;
for($i=0;$i<count($ary_filter_emails);$i++)
{
	$str_tmp.=$ary_filter_emails[$i]."','";
}
if($str_tmp!="")
{
	$str_filter=" and email not in('".$str_tmp."')";
}
if($readdb=="1")
{
	if(trim($_POST["sqlcondition"])!="")
		$str_sqlcond=" and ".str_replace("\'","'",$_POST["sqlcondition"]);
	else
		$str_sqlcond="";	
	$sql="select code,firstname,surname,email,country from cus_members where receive_news='n' ".$str_filter." and lower(country) like '%singapore%' ".$str_sqlcond." order by code";
	$arr=myQuery($sql);
}
for($i=0;$i<count($arr);$i++)
{
	$arr_tmp[$i]=$arr[$i][3];
}
$num=count($arr);
$code_other="M0000000";
if($readfile=="1")
{
	foreach($sg_user_info_ary as $key =>$value)
	{
		if(!in_array(trim($key),$ary_filter_emails) &&  (!in_array(trim($key),$arr_tmp)||!is_array($arr_tmp)))
		{
			$arr[$num][0]=$code_other;
			$arr[$num][1]="";
			$arr[$num][2]=$value;
			$arr[$num][3]=$key;
			$arr[$num][4]="Singapore";
			$num++;
		}
	}
}
for($i=0;$i<count($ary_other_emails);$i++)
{
	if(!in_array($ary_other_emails[$i],$ary_filter_emails) && (!in_array(trim($key),$arr_tmp)||!is_array($arr_tmp)))
	{
		$arr[$num+$i][0]=$code_other;
		$arr[$num+$i][1]="";
		$arr[$num+$i][2]="";
		$arr[$num+$i][3]=$ary_other_emails[$i];
	}
}

$web_root="http://".$_SERVER['HTTP_HOST'];
$old_imagepath=$_POST["old_imagepath"];
$new_imagepath=$web_root."/".$_POST["new_imagepath"];

$content=str_replace("{web_root}",$web_root,$content);
$content=str_replace($old_imagepath,$new_imagepath,$content);
$content=str_replace("\\\"","\"",$content);

if(isset($_POST["review"]))
{
	echo "<br/><table border=0 cellspacing=0 cellspadding=0 width=630 align=center><tr><td align=left>Subject: ".$subject."</td></tr></table>";
	echo "<br/>".$content;
	
	exit();	
}
if(isset($_POST["receiver"]))
{
	echo '<table width="600" border="0" cellpadding="0" cellspacing="1" bgcolor="#A3B2CC">
  			  			<tr>
  			  <td width="150" align="center">Code</td>
    		<td width="150" height="25" align="center">Name</td>
			<td width="100" height="25" align="center">Email</td>
			<td width="200" height="25" align="center">Country</td>
  			</tr>';
	for($i=0;$i<count($arr)&&$arr[0][0]!="";$i++)
	{
?>
  			<tr>
  			  <td width="150" align="left" bgcolor="#FFFFFF"><?=$arr[$i][0]?></td>
    		<td width="150" height="25" align="left" bgcolor="#FFFFFF"><?=$arr[$i][1]." ".$arr[$i][2]?></td>
			<td width="200" height="25" align="left" bgcolor="#FFFFFF"><?=$arr[$i][3]?></td>
			<td width="100" height="25" align="left" bgcolor="#FFFFFF"><?=$arr[$i][4]?></td>
  			</tr>
<?php
	}
	echo '</table>';
}
if(isset($_POST["send"]))
{
	for($i=0;$i<count($arr);$i++)
	{
		$to_name=$arr[$i][1].$arr[$i][2];
		$to_email=$arr[$i][3];
		$send_content=str_replace("{cf}",$cf,$content);
		$send_content=str_replace("{to_name}",$to_name,$send_content);
		$send_content=str_replace("{to_code}",$arr[$i][0],$send_content);
		$send_content=str_replace("{to_email}",$arr[$i][3],$send_content);		
		//sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$send_content);
		sendsmtpmail($from_name,$from_email,$to_name,$to_email,$subject,$send_content);
	}
	if($i>0)
	{
		echo "send email successfully! totals:".$i;
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
